package com.itheima.servlets.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import java.io.IOException;

@WebFilter(value = "/*",initParams = {@WebInitParam(name = "encoding",value = "UTF-8")})
public class EncodingFilter implements Filter {
    private String encoding;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //创建时获取配置的字符集编码格式
        encoding = filterConfig.getInitParameter("encoding");
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        //request和response字符集设置
        req.setCharacterEncoding(encoding);
        resp.setContentType("text/html;charset=" + encoding);
        //放行
        chain.doFilter(req,resp);
    }

    @Override
    public void destroy() {

    }
}
